package com.hinata.hinataimageloader;


import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class TeacherHomeworkActivity extends AppCompatActivity{
    private static final String TEACHER_HOMEWORK_URL = "http://172.27.35.2:8080/HiTeacher/homework/showHomework";
    private static final String TEACHER_HOMEWORK_URL_PRE="http://172.27.35.2:8080/HiTeacher/";
    String teacherId = "123456";
    String courseId = "1";
    String classId = "1";
    String studentId = "1504121860";
    String homeworkOrder = "2";
    List<Homework> listHomework = new ArrayList<>();
    TeacherHomeworkAdapter mAdapter;

    private Handler mHandler = new Handler();

    GridView gvTeacherHomework;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_homework);

        gvTeacherHomework = (GridView) findViewById(R.id.gv_teacher_homework);

        initData();
        mAdapter = new TeacherHomeworkAdapter(listHomework);
        gvTeacherHomework.setAdapter(mAdapter);
        gvTeacherHomework.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            }
        });




    }

    private void initData(){
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("teacherId", teacherId)
                .add("courseId", courseId)
                .add("classId", classId)
                .add("studentId", studentId)
                .add("homeworkOrder", homeworkOrder)
                .build();
        Request request = new Request.Builder()
                .url(TEACHER_HOMEWORK_URL)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("HINATA", "教师端请求作业失败");
                Log.e("HINATA", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonObject = response.body().string();
                Gson gson = new Gson();
                ResponseBean responseBean = gson.fromJson(jsonObject, ResponseBean.class);
                String homeworkUrls = responseBean.getPicUrl();
                String[] temp;
                temp = homeworkUrls.split(",");
                Homework homework;
                for (int i = 0; i < temp.length; i++) {
                    String imageUrl = TEACHER_HOMEWORK_URL_PRE+ temp[i];
                    String title = responseBean.getName() + responseBean.getCreatTime() + "-" + responseBean.getHomeworkOrder();
                    homework = new Homework(imageUrl, title);
                    listHomework.add(homework);
                }
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public class TeacherHomeworkAdapter extends BaseAdapter{

        private List<Homework> listHomework;
        private GridView gridView;

        public TeacherHomeworkAdapter(List<Homework> list) {
            super();
            this.listHomework = list;

        }

        @Override
        public int getCount() {
            return listHomework.size();
        }

        @Override
        public Object getItem(int position) {
            return listHomework.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (gridView == null) {
                gridView = (GridView) parent;
            }
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.item_gridview_homework, null);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_item_teacher_homework);
                holder.title = (TextView) convertView.findViewById(R.id.tv_item_teacher_homework);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Homework homework = listHomework.get(position);
            holder.title.setText(homework.getTitle());
            Glide.with(getApplicationContext())
                    .load(homework.getImageUrl())
                    .centerCrop()
                    .placeholder(R.drawable.papers)
                    .crossFade()
                    .into(holder.iv);

            return convertView;
        }

        class ViewHolder {
            ImageView iv;
            TextView title;
        }



    }

    public class Homework {
        private String imageUrl;
        private String title;

        public Homework(){

        }

        public Homework(String imageUrl,String title) {
            this.imageUrl = imageUrl;
            this.title=title;
        }

        public String getImageUrl() {
            return imageUrl;
        }
        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }

    }


}





